package com.pratices.mvc.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;

public class JsonUtil {
    //目的是实现Json对象和Java对象之间的转换
      //1.创建ObjectMapper对象
    private static ObjectMapper objectMapper = new ObjectMapper();

    //设置日期格式
    static {
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
    }
    public static String toJson(Object obj) throws JsonProcessingException {
        return objectMapper.writeValueAsString(obj);
    }
    public static <T> T parse(String json, Class<T> clazz) throws JsonProcessingException {
        return objectMapper.readValue(json, clazz);
    }
}
